/**
 * Title: jFamilyTree
 * Description: jFamilyTree mutable
 * Version: provider
 * Copyright: (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 * Update: Mon Mar 10 00:00:00 CEST 2008
 */
package jFamilyTree.model;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import jFamilyTree.model.INode;

/**
 * Extended Mutable node (mutable tree links, copy constructor)
 * 
 * @author Bernard Bou
 */
public class TreeMutableNode extends jFamilyTree.model.MutableNode
{
	// I D . F A C T O R Y

	/**
	 * Randomize used to generate ids
	 */
	static private Random theRandomizer = new Random();

	/**
	 * Make node id
	 * 
	 * @return node id
	 */
	static public String makeNodeId()
	{
		return "X" + Long.toHexString(theRandomizer.nextLong());
	}

	// C O N S T R U C T O R S

	/**
	 * Constructor
	 * 
	 * @param thisParent
	 *        parent
	 * @param thisId
	 *        id
	 */
	public TreeMutableNode(jFamilyTree.model.MutableNode thisParent, String thisId)
	{
		super(thisParent, thisId);
	}

	/**
	 * Copy constructor (the resulting node node has no tree parent nor tree children)
	 * 
	 * @param thatNode
	 *        node
	 */
	public TreeMutableNode(INode thatNode)
	{
		super(null, thatNode.getId());
		this.theChildren = null;

		this.theLabel = thatNode.getLabel();
		this.theContent = thatNode.getContent();
		this.theBackColor = thatNode.getBackColor();
		this.theForeColor = thatNode.getForeColor();
		this.theImageFile = thatNode.getImageFile();

		this.theEdgeLabel = thatNode.getEdgeLabel();
		this.theEdgeColor = thatNode.getEdgeColor();
		this.theEdgeStyle = thatNode.getEdgeStyle();
		this.theEdgeImageFile = thatNode.getEdgeImageFile();

		this.theLink = thatNode.getLink();
		this.theTarget = thatNode.getTarget();
		this.theMountPoint = thatNode.getMountPoint();
	}

	// I D

	/**
	 * Set id
	 * 
	 * @param thisId
	 *        id
	 */
	public void setId(String thisId)
	{
		theId = thisId;
	}

	// T R E E . L I N K S

	/**
	 * Set children
	 * 
	 * @param theseChildren
	 *        children
	 */
	public void setChildren(List<INode> theseChildren)
	{
		theChildren = theseChildren;
	}

	/**
	 * Add to parent (handles down link and uplink)
	 */
	public void addToParent(TreeMutableNode thisParent)
	{
		List<INode> theseChildren = thisParent.getChildren();
		if (theseChildren == null)
		{
			theseChildren = new ArrayList<INode>();
			thisParent.setChildren(theseChildren);
		}
		theseChildren.add(this);
		setParent(thisParent);
	}

	/**
	 * Remove from parent (handles down link and uplink)
	 */
	public void removeFromParent()
	{
		INode thisParent = getParent();
		if (thisParent != null)
		{
			thisParent.getChildren().remove(this);
			setParent(null);
		}
	}

	// M A K E . D E F A U L T

	/**
	 * Make default root node
	 * 
	 * @return root node
	 */
	public static INode makeDefaultTree()
	{
		final String[][] theData = { { "id1", "one" }, { "id2", "two" }, { "id3", "three" }, { "id4", "four" }, { "id5", "five" } };
		TreeMutableNode thisRoot = new TreeMutableNode(null, "root");
		thisRoot.setLabel("root");
		thisRoot.setBackColor(Color.RED);
		thisRoot.setForeColor(Color.BLACK);
		for (String[] theseStrings : theData)
		{
			TreeMutableNode thisNode = new TreeMutableNode(thisRoot, theseStrings[0]);
			thisNode.setLabel(theseStrings[1]);
			thisNode.setBackColor(Color.ORANGE);
			thisNode.setForeColor(Color.BLACK);
		}
		return thisRoot;
	}
}